package com.ncpowernode.javaweb.web;

import jakarta.servlet.*;
import jakarta.servlet.annotation.WebFilter;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;

@WebFilter("/book/*")
public class BookFilter implements Filter {
    @Override
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
            throws IOException, ServletException {

        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse)res;
        HttpSession session = request.getSession();
        if(session!=null&&session.getAttribute("user")!=null){
            chain.doFilter(request,response);
        }else{
            response.sendRedirect(request.getContextPath()+"/error.html");
        }

    }
}
